<template>
  <div>
    <el-row justify="end" class="title-add">
      <el-col :span='4' :offset=20 align=right>
        <el-button type="primary" @click="edit(null)">添加</el-button>
      </el-col>
    </el-row>
    <el-table
      :data="userList"
      stripe
      border
      fit
      style="width: 100%;">
      <el-table-column
        prop="user_name"
        align="center"
        label="姓名"
        width="100"
      >
      </el-table-column>
      <el-table-column
        prop="email"
        align="center"
        label="邮箱"
      >
      </el-table-column>
      <el-table-column
        prop="create_time"
        label="添加时间"
        align="center"
        width="100"
      >
      </el-table-column>
      <el-table-column
        prop="interview_time"
        align="center"
        label="面试时间"
        width="100"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="状态"
        width="90"
      >
      <template slot-scope="scope">
        <span>{{scope.row.interview_status == 0?'未发送':''}}</span>
        <span>{{scope.row.interview_status == 1?'已发送':''}}</span>
      </template>
      
      </el-table-column>
      <el-table-column
        align="center"
        width="250"
        label="操作">
        <template slot-scope="scope">
          <el-button @click="del(scope.row,scope.$index)" type="danger" size="small">删除</el-button>
          <el-button @click="edit(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="send(scope.row)" type="primary" size="small">发起面试</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="编辑/添加 用户" :visible.sync="dialogVisible" width="30%">
      <el-form label-width="80px" label-position="right">
        <el-form-item label="姓名">
          <el-input v-model="form.user_name"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 选择题目 -->
    <el-dialog :visible.sync="dialogSendVisible" title="选择题目">
     <el-table
        ref="multipleTable"
        :data="questionsList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="changeSelect"
      >
        <el-table-column
          prop="id"
          type="selection"
          align="center"
          width="55">
        </el-table-column>
        <el-table-column
          prop="title"
          label="题目"
          align="left"
          >
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogSendVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitSends()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "list",
  data() {
    return {
      userList: [],
      questionsList: [],
      align: "center",
      dialogVisible: false,
      dialogSendVisible: true,
      form: {
        user_name: "",
        email: ""
      },
      checkedItem: [],
      activeUser: null
    };
  },
  mounted() {
    this.getList();
    this.getQuestionsList();
  },
  methods: {
    async getQuestionsList(){
      let resArr = await this.$axios({
        url: '/questions/list'
      });
      this.questionsList = resArr.data.data.map((val)=>{
        val.answer = '';
        return val;
      });
    },
    async getList() {
      let loading = this.$loading();
      let resObj = await this.$axios({
        url: "/users/list",
        method: "get"
      });
      this.userList = resObj.data.data;
      loading.close();
    },
    async del(user, index) {
      await this.$axios({
        url: "/users/del",
        method: "get",
        params: {
          id: user.id
        }
      });
      this.userList.splice(index, 1);
    },
    edit(user) {
      this.dialogVisible = true;
      this.form.user_name = !!user ? user.user_name : null;
      this.form.email = !!user ? user.email : null;
      this.form.id = !!user ? user.id : null;
    },
    async submitEdit() {
      let loading = this.$loading();
      let resObj = await this.$axios({
        url: "/users/add",
        method: "post",
        data: this.form
      });
      loading.close();
      if (resObj.data.code == '1') {
        this.$message({
          message: resObj.data.msg,
          type: "error"
        });
      } else {
        this.dialogVisible = false;
        this.$message({
          message: "保存成功",
          type: "success"
        });
      }
      this.getList();
    },
    send(user) {
      // this.$confirm('?', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     this.$message({
      //       type: 'success',
      //       message: '删除成功!'
      //     });
      //   })
      this.dialogSendVisible = true;
    },
    changeSelect(val){
      this.checkedItem = val;
    },
    submitSends(){
      if(this.changeSelect.length < 1){
        this.$message('请选择题目');
        return;
      }
    }
  }
};
</script>
<style scoped>
.title-add {
  padding-bottom: 20px;
}
</style>


